package org.mijia.dotsbox;

import java.io.IOException;

import org.eclipse.jface.action.IStatusLineManager;
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.mijia.dotsbox.rcp.DotsboxActivator;

public class DotsboxAppGui {

	static class DotsboxAppGuiHolder {
		static DotsboxAppGui instance = new DotsboxAppGui();
	}
	
	private DotsboxAppGui() {
	}
	
	public static DotsboxAppGui getInstance() {
		return DotsboxAppGuiHolder.instance;
	}
	
	private IStatusLineManager statusManager = null;
    private Shell windowShell;
    
	public void initInstance(Shell windowShell) {
		this.windowShell = windowShell;
	}
	
	public void dispose() {
		
	}

	public static Image getImage(String path) {
		return DotsboxActivator.getImageDescriptor(path).createImage();
	}
	
	public Shell getShell() {
		return windowShell;
	}
	
	// status line foo
    public void setStatusLineManager(IStatusLineManager manager) {
        statusManager = manager;
    }
    
    public void setStatusMessage(String message) {
        if(statusManager != null) {
            final String fmessage = message;
            Display.getDefault().asyncExec(new Runnable() {
               public void run() {
                   statusManager.setMessage(fmessage);
                   statusManager.update(true);
               }
            });
        }
    }
    
    public void setStatusMessage(String message, Image image) {
        if(statusManager != null) {
            final String fmessage = message;
            final Image fimage = image;
            Display.getDefault().asyncExec(new Runnable() {
               public void run() {
                   statusManager.setMessage(fimage, fmessage);
                   statusManager.update(true);
               }
            });
        }
    }
    
    public void openDotsBoxHomepage() {
        doShowBrowser("http://mijia.org");
    }
    
    // open url in browser
    public boolean doShowBrowser(String url) {
        String os = System.getProperty("os.name").toLowerCase();
        Runtime rt = Runtime.getRuntime();
        try {
            if (os.indexOf("win") >= 0) {
                String[] cmd = new String[4];
                cmd[0] = "cmd.exe";
                cmd[1] = "/C";
                cmd[2] = "start";
                cmd[3] = url;
                rt.exec(cmd);
            } else if (os.indexOf("mac") >= 0) {
                rt.exec("open " + url);
            } else {
                String[] browsers = { "epiphany", "firefox", "mozilla",
                        "konqueror", "netscape", "opera", "links", "lynx" };

                StringBuffer cmd = new StringBuffer();
                for (int i = 0; i < browsers.length; i++)
                    cmd.append((i == 0 ? "" : " || ") + browsers[i] + " \""
                            + url + "\" ");

                rt.exec(new String[] { "sh", "-c", cmd.toString() });
            }
        } catch (IOException e) {
            e.printStackTrace();
            MessageDialog.openError(
                            null,
                            "Browser Error",
                            "The system failed to invoke your default web browser while attempting to access: \n\n "
                                    + url + "\n\n");
            return false;
        }
        return true;
    }
}
